package paas.storage.distributedColumnDatabase.impl;

import com.qingcloud.sdk.config.EnvContext;
import com.qingcloud.sdk.exception.QCException;
import com.qingcloud.sdk.service.InstanceService;
import com.qingcloud.sdk.service.Types;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import paas.storage.distributedColumnDatabase.IAuth;

/**
 * 备注
 *
 * @author xufeng
 * @email 525207937@qq.com
 * @date 2021/1/28 11:03
 */
@Service
public class IAuthImpl implements IAuth {

    private static Logger log = LoggerFactory.getLogger(IAuthImpl.class);


    @Override
    public InstanceService.DescribeInstancesOutput authentication(String accessKey, String accessSecret) {

        EnvContext context = new EnvContext(accessKey, accessSecret);
        InstanceService service = new InstanceService(context);
        InstanceService.DescribeInstancesInput input = new InstanceService.DescribeInstancesInput();
        input.setLimit(1);
        InstanceService.DescribeInstancesOutput output = null;
        try {
            output = service.describeInstances(input);
            if (output.getRetCode() == 1200) {
                log.error("ret code is " + output.getRetCode() + ", token is expired !");
                log.error("报错信息:" + output.getMessage());
            }
            if (log.isInfoEnabled()) {
                for (Types.InstanceModel model : output.getInstanceSet()) {
                    log.info("==================");
                    log.info(model.getInstanceID());
                    log.info(model.getInstanceName());
                    System.out.println(model.getImage().getImageID());
                    for (Types.NICVxNetModel vxNetModel : model.getVxNets()) {
                        log.info("==================");
                        log.info(vxNetModel.getVxNetID());
                        log.info(String.valueOf(vxNetModel.getVxNetType()));
                    }
                }
            }
        } catch (QCException e) {
            log.error(e.getMessage(), e);
        }
        return output;
    }
}
